<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class User extends Model
{
	protected $table = 'bolewang_admin_user';
    //用户登陆
	public function userlogin($param)
 	{
    	$email = User::get(['username'=>$param['username']]);
 		if(!isset($email)){
    		return json_encode(['message'=>'此账户不存在!','code'=>400]);
    	}
    	if($email['password']==pwd_md5($param['password'])){
    		return json_encode(['message'=>'用户密码错误!','code'=>400]);
    	}
    	$this->startTrans();
    	try{
    		$userinfo['account'] = $param['username'];
    		$userinfo['ip'] = $param['ip'];
            $data['id'] = $email['id'];
            $data['username'] = $email['username'];
    		Db::name('logininfo')->insertAll($userinfo);
            $this->commit();
    		return json_encode(['message'=>$email,'code'=>200]);
    	}catch(\Exception $e){
    		$this->rollback();
    		return json_encode(['message'=>'登陆表记录表失败','code'=>400]);
    	}
 	}
    //创建用户
    public function createuser($param)
    {
        $phone = User::get(['username'=>$param['username']]);
        $email = User::get(['company'=>$param['company']]);
        if(isset($phone)){
            return 'phonealr';
        }
        if(isset($email)){
            return 'emailealr';
        }
        $this->startTrans();
        try{
            $param['password'] = pwd_md5($param['password']);
            $this->data($param)->allowField(true)->save();
            $this->commit();
            return true;
        }catch(\Exception $e){
            $this->rollback();
            $this->error = '添加失败';
            return false;
        }
    }
    //更新用户
    public function updateuser($param)
    {
        $user = User::get($param['id']);
        $this->startTrans();
        try{
            $user['password'] = $param['password'];
            $user['rename'] = $param['rename'];
            $user['company'] = $param['company'];
            $user->allowField(true)->save();
            $this->commit();
            return true;
        }catch(\Exception $e){
            $this->rollback();
            $this->error = $e;
            return false;
        }
    }

    //删除用户
    public function deleteuser($param)
    {
        $user = User::get($param);
        $this->startTrans();
        try{
            $user->delete();
            $this->commit();
            return true;
        }catch(\Exception $e){
            $this->rollback();
            $this->error = '删除失败';
            return false;
        }
    }
    //激活用户
    public function fabuuser($param)
    {
        $user = User::get($param);
        $this->startTrans();
        try{
            $user['status'] = 1;
            $user->allowField(true)->save();
            $this->commit();
            return true;
        }catch(\Exception $e){
            $this->rollback();
            $this->error = '删除失败';
            return false;
        }
    }
    //禁用用户
    public function canceluser($param)
    {
        $user = User::get($param);
        $this->startTrans();
        try{
            $user['status'] = 0;
            $user->allowField(true)->save();
            $this->commit();
            return true;
        }catch(\Exception $e){
            $this->rollback();
            $this->error = '删除失败';
            return false;
        }
    }

}